package com.jdz.service.impl;

import com.jdz.common.ServerResponse;
import com.jdz.dao.*;
import com.jdz.pojo.*;
import com.jdz.service.IProjectDetailsRaceTypeSamsaraService;
import com.jdz.util.DateTimeUtil;
import com.jdz.util.NetworkUtil;
import com.jdz.vo.ProjectDetailsRaceTypeSamsaraVo;
import com.jdz.vo.ProjectDetailsTPVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Transactional
@Service("IProjectDetailsRaceTypeSamsaraService")
public class ProjectDetailsRaceTypeSamsaraServiceImpl implements IProjectDetailsRaceTypeSamsaraService {

    private Logger logger = LoggerFactory.getLogger(ProjectDetailsRaceTypeSamsaraServiceImpl.class);

    @Autowired
    ProjectDetailsRaceTypeSamsaraMapper projectDetailsRaceTypeSamsaraMapper;
    @Autowired
    ProjectDetailsMapper projectDetailsMapper;
    @Autowired
    RaceTypeMapper raceTypeMapper;
    @Autowired
    RaceTypeDetailsMapper raceTypeDetailsMapper;
    @Autowired
    RaceTypeSamsaraMapper raceTypeSamsaraMapper;
    @Autowired
    NetworkMapper networkMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ScoreReviewMapper scoreReviewMapper;

    /**
     * 查看本项目评审审核情况和票数
     * @param projectno
     * @return
     */
    public ServerResponse list(Integer projectno){
        if(projectno == null || projectno ==0){
            return ServerResponse.createByErrorMessage("参数错误");
        }

        ProjectDetails projectDetails = projectDetailsMapper.selectByPrimaryKey(projectno);
        if(projectDetails == null){
            return ServerResponse.createByErrorMessage("找不到此项目");
        }
        RaceType raceType = raceTypeMapper.selectByRaceType(projectDetails.getProjecttype());
        if(raceType == null){
            return ServerResponse.createByErrorMessage("找不到此项目类型");
        }
        RaceTypeDetails raceTypeDetails =  raceTypeDetailsMapper.selectByRacetypeno(raceType.getRacetypeno());
        if(raceType == null){
            return ServerResponse.createByErrorMessage("比赛没开始或者找不到此项目类型详情");
        }
        //RaceTypeSamsara raceTypeSamsara =  raceTypeSamsaraMapper.selectByRacetypenoAndRaceTypeSamsaraGrade(raceType.getRacetypeno(),raceTypeDetails.getRacetypedetailsstate());

        List<ProjectDetailsRaceTypeSamsara> projectDetailsRaceTypeSamsaraList = projectDetailsRaceTypeSamsaraMapper.selectByprojectDetailsNo(projectno);

        List<ProjectDetailsRaceTypeSamsaraVo> projectDetailsRaceTypeSamsaraVoList = new ArrayList<>();
        for(ProjectDetailsRaceTypeSamsara projectDetailsRaceTypeSamsaraItem : projectDetailsRaceTypeSamsaraList){

            RaceTypeSamsara raceTypeSamsaraItem = raceTypeSamsaraMapper.selectByPrimaryKey(projectDetailsRaceTypeSamsaraItem.getRacetypesamsarano());

            if(raceTypeSamsaraItem == null){
                return ServerResponse.createByErrorMessage("没有这个项目类型等级");
            }

            ProjectDetailsRaceTypeSamsaraVo projectDetailsRaceTypeSamsaraVo = new ProjectDetailsRaceTypeSamsaraVo();

            projectDetailsRaceTypeSamsaraVo.setProjraceno(projectDetailsRaceTypeSamsaraItem.getProjraceno());
            projectDetailsRaceTypeSamsaraVo.setReviewuser(projectDetailsRaceTypeSamsaraItem.getReviewuser());
            projectDetailsRaceTypeSamsaraVo.setReviewtime(projectDetailsRaceTypeSamsaraItem.getReviewtime());
            projectDetailsRaceTypeSamsaraVo.setReviewnotes(projectDetailsRaceTypeSamsaraItem.getReviewnotes());
            projectDetailsRaceTypeSamsaraVo.setRacetypesamsarano(projectDetailsRaceTypeSamsaraItem.getRacetypesamsarano());

            projectDetailsRaceTypeSamsaraVo.setRacetypesamsaraname(raceTypeSamsaraItem.getRacetypesamsaraname());
            projectDetailsRaceTypeSamsaraVo.setRacetypesamsaragrade(raceTypeSamsaraItem.getRacetypesamsaragrade());

            projectDetailsRaceTypeSamsaraVo.setProjectdetailsno(projectDetailsRaceTypeSamsaraItem.getProjectdetailsno());
            projectDetailsRaceTypeSamsaraVo.setBallot(projectDetailsRaceTypeSamsaraItem.getBallot());
            projectDetailsRaceTypeSamsaraVoList.add(projectDetailsRaceTypeSamsaraVo);
        }
        return ServerResponse.createBySuccess("返回成功",projectDetailsRaceTypeSamsaraVoList);
    }


    /**
     * 投票
     * @param projectno
     * @return
     */
    public ServerResponse ballot(HttpServletRequest request, Integer projectno){

        if(projectno == null || projectno ==0){
            return ServerResponse.createByErrorMessage("参数错误");
        }

        ProjectDetails projectDetails = projectDetailsMapper.selectByPrimaryKey(projectno);
        if(projectDetails == null){
            return ServerResponse.createByErrorMessage("找不到此项目");
        }
        RaceType raceType = raceTypeMapper.selectByRaceType(projectDetails.getProjecttype());
        if(raceType == null){
            return ServerResponse.createByErrorMessage("找不到此项目类型");
        }

        RaceTypeDetails raceTypeDetails =  raceTypeDetailsMapper.selectByRacetypeno(raceType.getRacetypeno());
        if(raceTypeDetails == null){
            return ServerResponse.createByErrorMessage("比赛没开始或者找不到此项目类型详情");
        }
        if(raceTypeDetails.getRacetypedetailsstate()==0){
            return ServerResponse.createByErrorMessage("比赛没开始");
        }

        RaceTypeSamsara raceTypeSamsara =  raceTypeSamsaraMapper.selectByRacetypenoAndRaceTypeSamsaraGrade(raceType.getRacetypeno(),raceTypeDetails.getRacetypedetailsstate());

        if(raceTypeSamsara==null){
            return ServerResponse.createByErrorMessage("比赛当前轮次在轮次表不存在此轮次，请联系程序员修改bug");
        }

        if(raceTypeSamsara.getBallotstatus()!=0){
            return ServerResponse.createByErrorMessage("投票没开始或已关闭");
        }

        ProjectDetailsRaceTypeSamsara projectDetailsRaceTypeSamsara =  projectDetailsRaceTypeSamsaraMapper.selectByprojectDetailsNoAndRacetypesamsarano(projectno,raceTypeSamsara.getRacetypesamsarano());




        Network network = new Network();
        String ip = null;
        try {
            ip = NetworkUtil.getIpAddr(request);
        } catch (Exception e) {
            return ServerResponse.createByErrorMessage("获取ip异常");
        }
        System.out.println(ip);
        if(StringUtils.isBlank(ip)){
            return ServerResponse.createByErrorMessage("获取ip失败");
        }

        List<Network> networkList = networkMapper.selectByIp(ip);
        for(Network Item : networkList){
            String nowDate = DateTimeUtil.dateToStr(new Date(),"yyyy-MM-dd");
            String ItemDate = DateTimeUtil.dateToStr(Item.getCreatime(),"yyyy-MM-dd");
            if(nowDate.equals(ItemDate)){
                return ServerResponse.createByErrorMessage("一天每人只能投票一次");
            }
        }

        //确认每次投票用户的mac地址和时间
        network.setIp(ip);
        network.setCreatime(new Date());
        int row = networkMapper.insertSelective(network);
        if(row<=0){
            return ServerResponse.createByErrorMessage("确认ip失败");
        }

        if(projectDetailsRaceTypeSamsara == null){


            ProjectDetailsRaceTypeSamsara projectDetailsRaceTypeSamsaraOne = new ProjectDetailsRaceTypeSamsara();
            projectDetailsRaceTypeSamsaraOne.setProjectdetailsno(projectno);
            projectDetailsRaceTypeSamsaraOne.setRacetypesamsarano(raceTypeSamsara.getRacetypesamsarano());
            projectDetailsRaceTypeSamsaraOne.setBallot(1);



            int row1 = projectDetailsRaceTypeSamsaraMapper.insertSelective(projectDetailsRaceTypeSamsaraOne);
            if(row1 <= 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ServerResponse.createByErrorMessage("投票失败");
            }
            return ServerResponse.createByErrorMessage("投票成功");


        }


        ProjectDetailsRaceTypeSamsara projectDetailsRaceTypeSamsaraOne = new ProjectDetailsRaceTypeSamsara();
        projectDetailsRaceTypeSamsaraOne.setProjraceno(projectDetailsRaceTypeSamsara.getProjraceno());
        projectDetailsRaceTypeSamsaraOne.setProjectdetailsno(projectno);
        projectDetailsRaceTypeSamsaraOne.setRacetypesamsarano(raceTypeSamsara.getRacetypesamsarano());
        projectDetailsRaceTypeSamsaraOne.setBallot(projectDetailsRaceTypeSamsara.getBallot()+1);
        int row2 = projectDetailsRaceTypeSamsaraMapper.updateByPrimaryKeySelective(projectDetailsRaceTypeSamsaraOne);
        if(row2 <= 0){
            return ServerResponse.createByErrorMessage("投票失败");
        }
        return ServerResponse.createBySuccessMessage("投票成功");

    }


    /**
     * 评审
     * @param projectnoList
     * @param stuno
     * @param reviewnotes
     * @param result
     * @param examinestatus
     * @return
     */
    public ServerResponse Review(List<Integer> projectnoList,Integer stuno,Integer reviewnotes,String result,Integer examinestatus){


        if(CollectionUtils.isEmpty(projectnoList)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        String reviewnotesString=null;
        if(!(reviewnotes==null)){
            try {
                reviewnotesString =  String.valueOf(reviewnotes);
            }catch (Exception e){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ServerResponse.createByErrorMessage("评审失败,分数格式不对");
            }
        }


        for(Integer Item : projectnoList){
            ProjectDetails projectDetails = projectDetailsMapper.selectByPrimaryKey(Item);
            if(projectDetails == null){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ServerResponse.createByErrorMessage("找不到此项目");
            }
            RaceType raceType = raceTypeMapper.selectByRaceType(projectDetails.getProjecttype());
            if(raceType == null){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ServerResponse.createByErrorMessage("找不到此项目类型");
            }

            RaceTypeDetails raceTypeDetails =  raceTypeDetailsMapper.selectByRacetypeno(raceType.getRacetypeno());
            if(raceTypeDetails == null||raceTypeDetails.getRacetypedetailsstate()==0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ServerResponse.createByErrorMessage("比赛没开始或者找不到此项目类型详情");
            }
            if(raceTypeDetails.getRacetypedetailsstate()==-1){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ServerResponse.createByErrorMessage("比赛已结束，无法评审");
            }

            RaceTypeSamsara raceTypeSamsara = raceTypeSamsaraMapper.selectByRacetypenoAndRaceTypeSamsaraGrade(raceType.getRacetypeno(),raceTypeDetails.getRacetypedetailsstate());
            if(raceTypeSamsara==null){
                return ServerResponse.createByErrorMessage("找不到当前比赛轮次");
            }




            ProjectDetailsRaceTypeSamsara projectDetailsRaceTypeSamsara =  projectDetailsRaceTypeSamsaraMapper.selectByprojectDetailsNoAndRacetypesamsarano(Item,raceTypeSamsara.getRacetypesamsarano());

            if(projectDetailsRaceTypeSamsara == null){

                ProjectDetailsRaceTypeSamsara projectDetailsRaceTypeSamsaraOne = new ProjectDetailsRaceTypeSamsara();
                projectDetailsRaceTypeSamsaraOne.setProjectdetailsno(Item);
                projectDetailsRaceTypeSamsaraOne.setRacetypesamsarano(raceTypeSamsara.getRacetypesamsarano());
                projectDetailsRaceTypeSamsaraOne.setReviewnotes(reviewnotesString);  //平均分
                //审核和审核人信息
                projectDetailsRaceTypeSamsaraOne.setExaminestatus(examinestatus);
                User u = userMapper.selectByPrimaryKey(stuno);
                projectDetailsRaceTypeSamsaraOne.setExamineuser(u.getStuname());
                projectDetailsRaceTypeSamsaraOne.setExaminetime(new Date());
                int row1 = projectDetailsRaceTypeSamsaraMapper.insertSelective(projectDetailsRaceTypeSamsaraOne);

                ProjectDetailsRaceTypeSamsara projectDetailsRaceTypeSamsara2 =  projectDetailsRaceTypeSamsaraMapper.selectByprojectDetailsNoAndRacetypesamsarano(Item,raceTypeSamsara.getRacetypesamsarano());
                if(projectDetailsRaceTypeSamsara2==null){
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return ServerResponse.createByErrorMessage("评审失败");
                }



                ScoreReview scoreReview = new ScoreReview();
                scoreReview.setReviewnotes(reviewnotes);
                scoreReview.setResult(result);
                scoreReview.setStuno(stuno);
                scoreReview.setProjraceno(projectDetailsRaceTypeSamsara2.getProjraceno());
                scoreReview.setCreattime(new Date());
                int srow = scoreReviewMapper.insertSelective(scoreReview);

                if(row1 <= 0||srow<=0){
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return ServerResponse.createByErrorMessage("评审失败");
                }
//                return ServerResponse.createByErrorMessage("评审成功");
            }else{


                ScoreReview scoreReview = new ScoreReview();
                scoreReview.setReviewnotes(reviewnotes);
                scoreReview.setResult(result);
                scoreReview.setStuno(stuno);
                scoreReview.setProjraceno(projectDetailsRaceTypeSamsara.getProjraceno());
                scoreReview.setCreattime(new Date());
                int srow2 = scoreReviewMapper.insertSelective(scoreReview);

                if(srow2<=0){
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return ServerResponse.createByErrorMessage("评审失败");
                }
                List<ScoreReview> scoreReviewList = scoreReviewMapper.selectByProjraceno(projectDetailsRaceTypeSamsara.getProjraceno());
                //算平均分
                Integer num=0;
                for(ScoreReview item : scoreReviewList){
                    num+=item.getReviewnotes();
                }
                num=num/scoreReviewList.size();
                try {
                    reviewnotesString =  String.valueOf(num);
                }catch (Exception e){
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return ServerResponse.createByErrorMessage("评审失败,分数格式不对");
                }

                ProjectDetailsRaceTypeSamsara projectDetailsRaceTypeSamsaraOne = new ProjectDetailsRaceTypeSamsara();
                projectDetailsRaceTypeSamsaraOne.setProjraceno(projectDetailsRaceTypeSamsara.getProjraceno());
                projectDetailsRaceTypeSamsaraOne.setReviewnotes(reviewnotesString);
                //审核和审核人信息
                projectDetailsRaceTypeSamsaraOne.setExaminestatus(examinestatus);
                User u = userMapper.selectByPrimaryKey(stuno);
                projectDetailsRaceTypeSamsaraOne.setExamineuser(u.getStuname());
                projectDetailsRaceTypeSamsaraOne.setExaminetime(new Date());
                int row2 = projectDetailsRaceTypeSamsaraMapper.updateByPrimaryKeySelective(projectDetailsRaceTypeSamsaraOne);
                if(row2 <= 0){
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return ServerResponse.createByErrorMessage("评审失败");
                }
//            return ServerResponse.createByErrorMessage("评审成功");
            }

        }

        return ServerResponse.createBySuccessMessage("评审成功");

    }


//    /**
//     * 审核
//     * @param projectnoList
//     * @param examineuser
//     * @param examinestatus
//     * @param examineResult
//     * @return
//     */
//    public ServerResponse Examine(List<Integer> projectnoList,String examineuser,Integer examinestatus,String examineResult,String reviewnotes ){
//        System.out.println(examineuser);
//        if(CollectionUtils.isEmpty(projectnoList) ||examinestatus == null|| StringUtils.isBlank(examineuser)){
//            return ServerResponse.createByErrorMessage("参数错误");
//        }
//        if(StringUtils.isBlank(reviewnotes)){
//            reviewnotes=null;
//        }
//        if(StringUtils.isBlank(examineResult)){
//            examineResult=null;
//        }
//        if(StringUtils.isBlank(examineuser)){
//            examineuser=null;
//        }
//        if(!(examinestatus==1||examinestatus==2||examinestatus==0)){
//            return ServerResponse.createByErrorMessage("审核状态参数不正确");
//        }
//
//        for(Integer Item : projectnoList){
//            ProjectDetails projectDetails = projectDetailsMapper.selectByPrimaryKey(Item);
//            if(projectDetails == null){
//                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//                return ServerResponse.createByErrorMessage("找不到此项目");
//            }
//            RaceType raceType = raceTypeMapper.selectByRaceType(projectDetails.getProjecttype());
//            if(raceType == null){
//                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//                return ServerResponse.createByErrorMessage("找不到此项目类型");
//            }
//            RaceTypeDetails raceTypeDetails =  raceTypeDetailsMapper.selectByRacetypeno(raceType.getRacetypeno());
//            if(raceTypeDetails == null||raceTypeDetails.getRacetypedetailsstate()==0){
//                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//                return ServerResponse.createByErrorMessage("比赛没开始或者找不到此项目类型详情");
//            }
//
//            RaceTypeSamsara raceTypeSamsara =  raceTypeSamsaraMapper.selectByRacetypenoAndRaceTypeSamsaraGrade(raceType.getRacetypeno(),raceTypeDetails.getRacetypedetailsstate());
//
//            if(raceTypeSamsara==null){
//                return ServerResponse.createByErrorMessage("比赛轮次不存在");
//            }
//
//
//            ProjectDetailsRaceTypeSamsara projectDetailsRaceTypeSamsara =  projectDetailsRaceTypeSamsaraMapper.selectByprojectDetailsNoAndRacetypesamsarano(Item,raceTypeSamsara.getRacetypesamsarano());
//
//            if(projectDetailsRaceTypeSamsara == null){
//
//                ProjectDetailsRaceTypeSamsara projectDetailsRaceTypeSamsaraOne = new ProjectDetailsRaceTypeSamsara();
//                projectDetailsRaceTypeSamsaraOne.setProjectdetailsno(Item);
//                projectDetailsRaceTypeSamsaraOne.setRacetypesamsarano(raceTypeSamsara.getRacetypesamsarano());
//                projectDetailsRaceTypeSamsaraOne.setExamineuser(examineuser);
//                projectDetailsRaceTypeSamsaraOne.setExaminestatus(examinestatus);
//                projectDetailsRaceTypeSamsaraOne.setExamineresult(examineResult);
//                projectDetailsRaceTypeSamsaraOne.setReviewnotes(reviewnotes);
//                projectDetailsRaceTypeSamsaraOne.setExaminetime(new Date());
//                if(reviewnotes!=null){
//                    projectDetailsRaceTypeSamsaraOne.setReviewuser(examineuser);
//                    projectDetailsRaceTypeSamsaraOne.setReviewtime(new Date());
//                }
//                int row1 = projectDetailsRaceTypeSamsaraMapper.insertSelective(projectDetailsRaceTypeSamsaraOne);
//                if(row1 <= 0){
//                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//                    return ServerResponse.createByErrorMessage("审核失败");
//                }
////                return ServerResponse.createByErrorMessage("审核成功");
//            }else{
//                ProjectDetailsRaceTypeSamsara projectDetailsRaceTypeSamsaraOne = new ProjectDetailsRaceTypeSamsara();
//                projectDetailsRaceTypeSamsaraOne.setProjraceno(projectDetailsRaceTypeSamsara.getProjraceno());
//                projectDetailsRaceTypeSamsaraOne.setExamineuser(examineuser);
//                projectDetailsRaceTypeSamsaraOne.setExaminestatus(examinestatus);
//                projectDetailsRaceTypeSamsaraOne.setExamineresult(examineResult);
//                projectDetailsRaceTypeSamsaraOne.setReviewnotes(reviewnotes);
//                projectDetailsRaceTypeSamsaraOne.setExaminetime(new Date());
//                if(reviewnotes!=null){
//                    projectDetailsRaceTypeSamsaraOne.setReviewuser(examineuser);
//                    projectDetailsRaceTypeSamsaraOne.setReviewtime(new Date());
//                }
//                int row2 = projectDetailsRaceTypeSamsaraMapper.updateByPrimaryKeySelective(projectDetailsRaceTypeSamsaraOne);
//                if(row2 <= 0){
//                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//                    return ServerResponse.createByErrorMessage("审核失败");
//                }
//            }
//
////            return ServerResponse.createByErrorMessage("审核成功");
//        }
//
//        return ServerResponse.createBySuccessMessage("审核成功");
//    }

    /**
     * 获取此项目所有项目
     * @param racetypeno
     * @return
     */
    public ServerResponse list_racetypeno(Integer racetypeno){



        RaceType raceType = raceTypeMapper.selectByPrimaryKey(racetypeno);
        if(raceType==null){
            return ServerResponse.createByErrorMessage("没有此类型");
        }


        RaceTypeDetails raceTypeDetails = raceTypeDetailsMapper.selectByRacetypeno(racetypeno);

        if(raceTypeDetails==null){
            return ServerResponse.createByErrorMessage("该比赛轮次还没开始");
        }else if(raceTypeDetails.getRacetypedetailsstate()==0){
            return ServerResponse.createByErrorMessage("该比赛轮次还没开始");
        }else if(raceTypeDetails.getRacetypedetailsstate()==-1) {
            return ServerResponse.createByErrorMessage("该比赛轮次还已结束");
        }

        RaceTypeSamsara raceTypeSamsara = raceTypeSamsaraMapper.selectByRacetypenoAndRaceTypeSamsaraGrade(racetypeno,raceTypeDetails.getRacetypedetailsstate());

        if(raceTypeSamsara==null){
            return ServerResponse.createByErrorMessage("本比赛轮次不存在");
        }




        //得到这个项目类型的所有项目
        List<ProjectDetails> projectDetailsList = projectDetailsMapper.selectByProjectRaceType(raceType.getRacetype());
        List<ProjectDetailsTPVo> projectDetailsTPVoList =  new ArrayList<>();

        for(ProjectDetails projectDetailsItem : projectDetailsList){
            ProjectDetailsTPVo projectDetailsTPVo = new ProjectDetailsTPVo();
            projectDetailsTPVo.setProjectno(projectDetailsItem.getProjectno());
            projectDetailsTPVo.setProjectname(projectDetailsItem.getProjectname());
            projectDetailsTPVo.setCoverurl(projectDetailsItem.getcoverurl());
            projectDetailsTPVo.setTeam(projectDetailsItem.getTeam());

            ProjectDetailsRaceTypeSamsara projectDetailsRaceTypeSamsara = projectDetailsRaceTypeSamsaraMapper.selectByprojectDetailsNoAndRacetypesamsarano(projectDetailsItem.getProjectno(),raceTypeSamsara.getRacetypesamsarano());
            if(projectDetailsRaceTypeSamsara==null){
                projectDetailsTPVo.setBallot(0);
            }else {
                projectDetailsTPVo.setBallot(projectDetailsRaceTypeSamsara.getBallot());
            }




            int iff = 0;
            //判断前面是否及格，是否要显示
            for(int i=1;i<raceTypeDetails.getRacetypedetailsstate();i++){
                RaceTypeSamsara raceTypeSamsaraIf = raceTypeSamsaraMapper.selectByRacetypenoAndRaceTypeSamsaraGrade(raceType.getRacetypeno(),i);
                if(raceTypeSamsaraIf==null){
                    iff=1;
                    break;
                }
                ProjectDetailsRaceTypeSamsara pif = projectDetailsRaceTypeSamsaraMapper.selectByprojectDetailsNoAndRacetypesamsarano(projectDetailsItem.getProjectno(),raceTypeSamsaraIf.getRacetypesamsarano());
                if(pif==null){
                    iff=1;
                    break;
                }
                if(pif.getExaminestatus()!=1){
                    iff=1;
                    break;
                }
            }

            if(iff==1) {
                continue;
            }

            RaceTypeSamsara raceTypeSamsara2 = raceTypeSamsaraMapper.selectByRacetypenoAndRaceTypeSamsaraGrade(raceType.getRacetypeno(),raceTypeDetails.getRacetypedetailsstate());
            if(raceTypeSamsara2==null){
                continue;
            }
            ProjectDetailsRaceTypeSamsara p = projectDetailsRaceTypeSamsaraMapper.selectByprojectDetailsNoAndRacetypesamsarano(projectDetailsItem.getProjectno(),raceTypeSamsara2.getRacetypesamsarano());

            if (p==null){
                continue;
            }
            //如果审核没通过，通过时=1
            if(p.getExaminestatus()!=1){
                continue;
            }
            projectDetailsTPVoList.add(projectDetailsTPVo);

        }



        return ServerResponse.createBySuccess(raceType.getRacetype()+"第"+raceTypeDetails.getRacetypedetailsstate()+"轮比赛",projectDetailsTPVoList);
    }

}
